using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SecurityLinkUp
{
    public partial class ViewChapter : System.Web.UI.Page
    {
        private Guid ChapterID;

        protected void Page_Load(object sender, EventArgs e)
        {
            // get the chapter ID from the querystring
            ChapterID = new Guid(Page.Request.QueryString["ChapterID"]);

            // lookup the chapter
            Chapter c = ChapterData.GetChapter(ChapterID);

            // populate the details if we have a valid chapter
            if (c != null)
            {
                ChapterName.Text = c.name;
                ChapterDescription.Text = c.description;

                MembersCount.Text = ChapterData.GetChapterMembersCount(ChapterID).ToString() + " members";
                MeetingsCount.Text = MeetingData.GetChapterMeetingsCount(ChapterID).ToString() + " meetings";
                Meeting m = MeetingData.GetNextMeeting(ChapterID);
                if (m != null)
                {
                    NextMeeting.Text = "Next meeting on " + m.Start.ToShortDateString();
                }
                else
                {
                    NextMeeting.Text = "No future meetings planned";
                }

                // populate the meetings calendar
                ArrayList meetings = MeetingData.GetAllChapterMeetings(ChapterID);
                foreach (Meeting meeting in meetings)
                {
                    MeetingsCalendar.SelectedDates.Add(meeting.Start);
                }

                // set the "join chapter" link
                MembershipUser usr = Membership.GetUser();
                if (usr != null)
                {
                    // see if already a member
                    if (ChapterData.IsUserChapterMember(ChapterID, (Guid)usr.ProviderUserKey))
                    {
                        JoinLink.Text = "You are a member of this chapter";
                        JoinLink.PostBackUrl = "";
                    }
                    else
                    {
                        JoinLink.Text = "Join this chapter";
                        JoinLink.PostBackUrl = "";
                        
                    }
                }
                else
                {
                    JoinLink.Text = "Sign-in to become a member of this chapter";
                    JoinLink.PostBackUrl = "/login.aspx?ReturnUrl=%2fViewChapter.aspx%3fChapterID%3d" + ChapterID;
                    
                }
            }

        }

        protected void MeetingsCalendar_SelectionChanged(object sender, EventArgs e)
        {
            // get the selected date
            DateTime SelectedDate = MeetingsCalendar.SelectedDate;

            // find the meeting on the selected date
            DateTime StartDate = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, 0, 0, 0);
            DateTime EndDate = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, 23, 59, 59);
            ArrayList meetings = MeetingData.GetChapterMeetingsBetweenDates(ChapterID, StartDate, EndDate);

            // get the first meetings
            // TODO: could be more that one meeting (prehaps overlapping) in the future
            // sho should popup something that lets the user select which one they want
            if ((meetings != null) && (meetings.Count > 0))
            {
                Meeting m = (Meeting)meetings[0];
                User u = UserData.GetUser(m.OrganizerID);
                Organizer.Text = u.name;
                MeetingTitle.Text = m.Title;
                Start.Text = m.Start.ToShortDateString() + " " + m.Start.ToShortTimeString();
                End.Text = m.End.ToShortDateString() + " " + m.End.ToShortTimeString();
                Description.Text = m.Description;
                location.Text = m.Location;
                MeetingLink.Text = "Click here to go to the meeting's webpage";
                MeetingLink.NavigateUrl = "~/ViewMeeting.aspx?MeetingID=" + m.uid;
            }
            else
            {
                ClearMeetingData();
            }
        }

        protected void ClearMeetingData()
        {
            Organizer.Text = "";
            MeetingTitle.Text = "";
            Start.Text = "";
            End.Text = "";
            Description.Text = "";
            location.Text = "";
            MeetingLink.Text = "";
            MeetingLink.NavigateUrl = "";
        }

        protected void JoinLink_Click(object sender, EventArgs e)
        {
            // ensure that the user is signed in
            MembershipUser usr = Membership.GetUser();
            if (usr != null)
            {
                // make the user a member of this chapter
                bool ok = ChapterData.JoinChapter(ChapterID, (Guid)usr.ProviderUserKey);

                // if the operation went ok, change the link message
                if (ok)
                {
                    JoinLink.Text = "You are a member of this chapter";
                    JoinLink.PostBackUrl = "";
                }
            }
        }
    }
}
